package com.cf.wmsg.controller;

import com.cf.constant.EnumReturnCode;
import com.cf.user.login.model.UserInfo;
import com.cf.user.login.service.TokenManager;
import com.cf.vo.ResponseVo;
import com.cf.web.RequiredLogin;
import com.cf.wmsg.controller.vo.WebMessageVo;
import com.cf.wmsg.model.WebMessage;
import com.cf.wmsg.model.WebMessageService;
import com.github.pagehelper.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * boot-message-WebMessageFrontController
 *
 * @author <a href="2428922347@qq.com">Zhu mingming</a>
 * @info
 * @since 2018/7/12 下午 04:36
 */
@Api("网页消息")
@RestController
@RequestMapping("/webMessage")
public class WebMessageFrontController {

    @Autowired
    private TokenManager tokenManager;

    @Autowired
    private WebMessageService webMessageService;


    @RequiredLogin
    @ApiOperation(value = "获取用户的信息")
    @RequestMapping(value = "/userMessage", method = {RequestMethod.GET})
    public ResponseVo<List<WebMessage>> userWebMessage(HttpServletRequest httpServletRequest, @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "20") int size) {
        UserInfo userInfo = tokenManager.userInfo(httpServletRequest);
        if (userInfo == null) {
            return ResponseVo.fail(EnumReturnCode.NEED_LOGIN.getCode(), null, "登陆失效，请重新登陆！");
        }
        Page<WebMessage> webMessagePage = webMessageService.getUserMessage(userInfo.getId(), page, size);
        return ResponseVo.success(webMessagePage, "获取消息成功！");
    }

    @RequiredLogin
    @ApiOperation(value = "获取用户的信息-已经分组了")
    @RequestMapping(value = "/userMessage/group", method = {RequestMethod.GET})
    public ResponseVo<List<WebMessageVo>> groupUserWebMessage(HttpServletRequest httpServletRequest) {
        UserInfo userInfo = tokenManager.userInfo(httpServletRequest);
        if (userInfo == null) {
            return ResponseVo.fail(EnumReturnCode.NEED_LOGIN.getCode(), null, "登陆失效，请重新登陆！");
        }
        Page<WebMessage> webMessagePage = webMessageService.getUserMessage(userInfo.getId(), 1, Integer.MAX_VALUE);
        List<WebMessage> webMessages = webMessagePage.getResult();
        return ResponseVo.success(groupMessage(webMessages), "获取消息成功！");
    }

    private List<WebMessageVo> groupMessage(List<WebMessage> webMessages) {

        List<WebMessageVo> webMessageVos = new LinkedList<>();
        Map<String, List> messageMap = new HashMap();

        webMessages.forEach(webMessage -> {
            List list = messageMap.get(webMessage.getName());
            if (list == null) {
                list = new LinkedList();
                messageMap.put(webMessage.getName(), list);
            }
            list.add(webMessage);
        });

        webMessages.forEach(webMessage -> {
            List list = messageMap.get(webMessage.getName());
            if (list == null) {
                return;
            }
            WebMessageVo webMessageVo = new WebMessageVo();
            webMessageVo.setTitle(webMessage.getName());
            webMessageVo.setType(webMessage.getType());
            webMessageVo.setWebMessageList(list);
            webMessageVos.add(webMessageVo);
            messageMap.remove(webMessage.getName());
        });

        return webMessageVos;
    }

    @RequiredLogin
    @ApiOperation(value = "将消息设置为已查看")
    @RequestMapping(value = "/watch", method = {RequestMethod.GET})
    public ResponseVo<Boolean> watch(long messageId, HttpServletRequest request) {
        UserInfo userInfo = tokenManager.userInfo(request);
        if (userInfo == null) {
            return ResponseVo.fail(EnumReturnCode.NEED_LOGIN.getCode(), null, "登陆失效，请重新登陆！");
        }
        Boolean hasWatched = webMessageService.watch(messageId, userInfo.getId());
        return ResponseVo.success(hasWatched, "处理成功！");
    }

    @RequiredLogin
    @ApiOperation(value = "删除消息")
    @RequestMapping(value = "/del", method = {RequestMethod.GET})
    public ResponseVo<List<WebMessage>> delWebMessage(long messageId, HttpServletRequest request) {
        UserInfo userInfo = tokenManager.userInfo(request);
        if (userInfo == null) {
            return ResponseVo.fail(EnumReturnCode.NEED_LOGIN.getCode(), null, "登陆失效，请重新登陆！");
        }
        Boolean hasRemoved = webMessageService.delWebMessage(messageId, userInfo.getId());
        return ResponseVo.success(hasRemoved, "处理成功！");
    }


}
